package com.nytd.demo.service;

import com.nytd.demo.entity.BookInfo;
import com.nytd.demo.entity.BookStatus;
import com.nytd.demo.entity.PageRequest;
import com.nytd.demo.entity.PageResult;
import com.nytd.demo.mapper.BookInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookInfoService {
    @Autowired
    private BookInfoMapper bookInfoMapper;

    public List<BookInfo> getList(){
        List<BookInfo> books = bookInfoMapper.select();
        for (BookInfo book : books) {
            if (book.getStatus() == 1) {
                book.setStatusCN("可借阅");
            } else {
                book.setStatusCN("不可借阅");
            }
        }
        return books;
    }

    public Integer addBook(BookInfo bookInfo){
        return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        Integer count = bookInfoMapper.count();
        List<BookInfo> books = bookInfoMapper.selectByPage(pageRequest);
        for (BookInfo book : books) {
            book.setStatusCN(BookStatus.getNameByCode(book.getStatus()));
        }
        return new PageResult<>(count, books, pageRequest);
    }

    public BookInfo selectBookById(Integer bookId) {
        return bookInfoMapper.selectBookById(bookId);
    }

    public Integer updateBookById(BookInfo bookInfo){
        return bookInfoMapper.updateBookById(bookInfo);
    }

    public Integer deleteBookById(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(BookStatus.DELETED.getCode());
        Integer result = bookInfoMapper.updateBookById(bookInfo);
        return result;
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookInfoMapper.batchDeleteBook(ids);
    }
}
